package com.jfok.server.controller.report;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jfok.server.common.ActionResult;
import com.jfok.server.common.TreeValueText;
import com.jfok.server.login.SessionManage;
import com.jfok.server.service.ReportConditionService;
import com.jfok.shared.TreeNodeRecordChecked;

@Controller
@RequestMapping("/report")
public class ReportController {

	@Resource
	private ReportConditionService reportConditionService;

	
	@RequestMapping("/fetchdata.do")
	@ResponseBody
	public Map<String, Object> fetchData(String reportGroupId, Integer reportId, Integer start,
			Integer limit, String sort, String query, String selectedFields, String baseModuleName,
			String moduleConditions, String filter, String groupFields, Boolean groupShowDetail,
			Boolean isShowTotal, HttpServletRequest request) {

		return reportConditionService.fetchData(reportGroupId, reportId, start, limit, sort, query,
				baseModuleName, selectedFields, moduleConditions, filter, isShowTotal, groupFields,
				groupShowDetail, request);

	}

	
	@RequestMapping("/downloadresult.do")
	@ResponseBody
	public void downloadResult(String reportGroupId, Integer reportId, Integer start, Integer limit,
			String sort, String query, String selectedFields, String baseModuleName,
			String moduleConditions, String filter, String groupFields, Boolean groupShowDetail,
			Boolean isShowTotal, Boolean iswanyuan, String hiddenColumns, Boolean ispdf, Boolean print,
			HttpServletRequest request, HttpServletResponse response) throws IOException {

		reportConditionService.downloadResult(reportGroupId, reportId, start, limit, sort, query,
				baseModuleName, selectedFields, moduleConditions, filter, isShowTotal, groupFields,
				groupShowDetail, iswanyuan, hiddenColumns, ispdf, print, request, response);

	}

	
	@RequestMapping("/getfactgroupandfields.do")
	@ResponseBody
	public Object getFactGroupAndFields(String baseModuleName, String fields, String groupFields,
			Boolean groupShowDetail, HttpServletRequest request) {
		return reportConditionService.getFactGroupAndFields(baseModuleName, fields, groupFields,
				groupShowDetail, request);
	}

	
	@RequestMapping("/getconditionsrecordcount.do")
	@ResponseBody
	public Map<String, Object> getConditionsRecordCount(String baseModuleName, String fields,
			String moduleConditions, HttpServletRequest request) {
		return reportConditionService.getConditionsRecordCount(baseModuleName, fields,
				moduleConditions, request);
	}

	
	@RequestMapping("/validselectedfields.do")
	@ResponseBody
	public ActionResult validSelectedFields(String fields, HttpServletRequest request) {

		return reportConditionService.validSelectedFields(fields, request);

	}

	

	@RequestMapping("/getreportinfo.do")
	@ResponseBody
	public Object getReportInfo(Integer reportId, HttpServletRequest request) {

		return reportConditionService.getReportInfo(reportId, request);

	}

	

	@RequestMapping("/savereport.do")
	@ResponseBody
	public ActionResult SaveReport(Integer reportId, String selectedFields, String baseModuleName,
			Boolean isShowTotal, String groupFields, Boolean groupShowDetail, HttpServletRequest request) {
		if (reportConditionService.isSystemReport(reportId)) {
			if (!SessionManage.getInstance().getUserSession(request.getSession()).getLoginName()
					.equals("admin"))
				return new ActionResult(false, "系统内置的综合查询方案只能由“系统管理员”进行修改。");
		}

		reportConditionService.deleteFieldGroupWithReportId(reportId);
		reportConditionService.updateReportInfo(reportId, baseModuleName, isShowTotal, groupFields,
				groupShowDetail);
		return reportConditionService.setReportGroupAndField(reportId, selectedFields, request);
	}

	

	@RequestMapping("/saveasreport.do")
	@ResponseBody
	public Object saveAsReport(String reportGroupId, String text, String selectedFields,
			String baseModuleName, Boolean isShowTotal, String groupFields, Boolean groupShowDetail,
			HttpServletRequest request) {

		return reportConditionService.saveAsReport(reportGroupId, text, selectedFields, baseModuleName,
				isShowTotal, groupFields, groupShowDetail, request);
	}

	@RequestMapping("/deletereport.do")
	@ResponseBody
	public ActionResult deleteReport(Integer reportId, HttpServletRequest request) {

		return reportConditionService.deleteReport(reportId, request);
	}

	
	@RequestMapping("/getgroupreports.do")
	@ResponseBody
	public List<TreeValueText> getGroupReports(String reportGroupId, HttpServletRequest request) {

		return reportConditionService.getGroupReports(reportGroupId, request);

	}

	@RequestMapping("/fetchmoduleconitiontree.do")
	
	public @ResponseBody
	List<TreeNodeRecordChecked> getModuleTreeData(String conditionId, HttpServletRequest request) {
		return reportConditionService.getModuleTreeData(conditionId, request);

	}

	@RequestMapping("/fetchgroupandmodule.do")
	
	public @ResponseBody
	List<TreeValueText> getModuleGroupAndModule(HttpServletRequest request) {
		return reportConditionService.getModuleGroupAndModule(request);

	}

	@RequestMapping("/fetchmodulefields.do")
	
	public @ResponseBody
	List<TreeValueText> getModuleFields(String moduleName, HttpServletRequest request) {
		return reportConditionService.getModuleFields(moduleName, request);

	}

}
